
function format_date(d, fmt, monthNames) {
    var leftPad = function(n) {
	n = "" + n;
	return n.length == 1 ? "0" + n : n;
    };
    
    var r = [];
    var escape = false;
    if (monthNames == null)
	monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    for (var i = 0; i < fmt.length; ++i) {
	var c = fmt.charAt(i);
	
	if (escape) {
	    switch (c) {
	    case 'h': c = "" + d.getUTCHours(); break;
	    case 'H': c = leftPad(d.getUTCHours()); break;
	    case 'M': c = leftPad(d.getUTCMinutes()); break;
	    case 'S': c = leftPad(d.getUTCSeconds()); break;
	    case 'd': c = "" + d.getUTCDate(); break;
	    case 'm': c = "" + (d.getUTCMonth() + 1); break;
	    case 'y': c = "" + d.getUTCFullYear(); break;
	    case 'b': c = "" + monthNames[d.getUTCMonth()]; break;
	    }
	    r.push(c);
	    escape = false;
	}
	else {
	    if (c == "%")
		escape = true;
	    else
		r.push(c);
	}
    }
    return r.join("");
}
